#include<bits/stdc++.h>
using namespace std;
long long T, n, a[200005], pre[200005], num[1000005], dp[200005][2];
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    scanf("%lld", &T);
    while(T--)
    {
        memset(pre, 0, sizeof pre);
        memset(num, 0, sizeof num);
        memset(dp, 0, sizeof dp);
        scanf("%lld", &n);
        long long ans = 0;
        for(long long i = 1; i <= n; i++)
        {
            scanf("%lld", a + i);
            pre[i] = num[a[i]];
            if(i > 1 && pre[i] == i - 1)
            {
                pre[i] = 0;
                ans += a[i];
            }
            num[a[i]] = i;
        }
        for(long long i = 1; i <= n; i++)
        {
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
            if(pre[i])
                dp[i][1] = max(dp[pre[i] + 1][0], dp[pre[i] + 1][1]) + a[i];
        }
        printf("%lld\n", ans + max(dp[n][0], dp[n][1]));
    }
    return 0;
}
